/*
 *  Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
 *  <p>
 *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  <p>
 * https://www.gnu.org/licenses/lgpl.html
 *  <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhang.gateway.filter;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhang.common.core.config.FilterIgnorePropertiesConfig;
import com.zhang.common.core.constant.ResultConstant;
import com.zhang.common.core.constant.MsgTemplate;
import com.zhang.common.core.constant.RedisKeyConstant;
import com.zhang.common.core.constant.SecurityConstant;
import com.zhang.common.core.exception.ValidateCodeException;
import com.zhang.common.core.util.WebUtils;
import com.zhang.common.core.vo.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author lengleng
 * @date 2018/7/4
 * 验证码处理
 */
@Slf4j
@Component
@AllArgsConstructor
public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
	private final ObjectMapper objectMapper;
	private final RedisTemplate redisTemplate;
	private final FilterIgnorePropertiesConfig filterIgnorePropertiesConfig;
//	private final LogService logService;


	@Override
	public GatewayFilter apply(Object config) {
		return (exchange, chain) -> {
			ServerHttpRequest request = exchange.getRequest();

			// 不是登录请求，直接向下执行
			if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath()
				, SecurityConstant.OAUTH_TOKEN_URL)) {
				return chain.filter(exchange);
			}

			// 刷新token，直接向下执行
			String grantType = request.getQueryParams().getFirst("grant_type");
			if (StrUtil.equals(SecurityConstant.REFRESH_TOKEN, grantType)) {
				return chain.filter(exchange);
			}

			// 终端设置不校验， 直接向下执行
			try {
				String[] clientInfos = WebUtils.getClientId(request);
				if (filterIgnorePropertiesConfig.getClients().contains(clientInfos[0])) {
					return chain.filter(exchange);
				}

				//校验验证码
				checkCode(request);

			} catch (ValidateCodeException e) {
				ServerHttpResponse response = exchange.getResponse();
				response.setStatusCode(HttpStatus.OK);
				HttpHeaders headers = response.getHeaders();
				headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//				logService.saveGateWayErrorLog(e.getMessage());
				try {
					return response.writeWith(Mono.just(response.bufferFactory()
							.wrap(objectMapper.writeValueAsBytes(
									new Result(e.getCode(),e.getMessage())
							))));
				} catch (JsonProcessingException e1) {
					log.error("对象输出异常", e1);
					e1.printStackTrace();
				}

			} catch (Exception e) {
				ServerHttpResponse response = exchange.getResponse();
				response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);

				try {
					HttpHeaders headers = response.getHeaders();
					headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
					return response.writeWith(Mono.just(response.bufferFactory()
						.wrap(objectMapper.writeValueAsBytes(
								new Result(ResultConstant.EX_OTHER_CODE,e.getMessage())
						))));
				} catch (JsonProcessingException e1) {
					log.error("对象输出异常", e1);
				}
			}

			return chain.filter(exchange);
		};
	}

	/**
	 * 检查code
	 *
	 * @param request
	 */

	private void checkCode(ServerHttpRequest request) {
		String code = request.getQueryParams().getFirst("code");

		if (StrUtil.isBlank(code)) {
			throw new ValidateCodeException(MsgTemplate.NULL_VALIDATE_CODE);
		}

		String randomStr = request.getQueryParams().getFirst("randomStr");
//		if (StrUtil.isBlank(randomStr)) {
//			randomStr = request.getQueryParams().getFirst("mobile");
//		}

		String key = RedisKeyConstant.DEFAULT_CODE_KEY + randomStr;
		if (!redisTemplate.hasKey(key)) {
			throw new ValidateCodeException(MsgTemplate.ERROR_VALIDATE_CODE);
		}

		Object codeObj = redisTemplate.opsForValue().get(key);

		if (codeObj == null) {
//			logService.saveGateWayErrorLog(MsgTemplate.ERROR_VALIDATE_CODE);

			throw new ValidateCodeException(MsgTemplate.ERROR_VALIDATE_CODE);
		}

		String saveCode = codeObj.toString();
		if (StrUtil.isBlank(saveCode)) {
			redisTemplate.delete(key);
			throw new ValidateCodeException(MsgTemplate.ERROR_VALIDATE_CODE);
		}

		if (!StrUtil.equals(saveCode, code)) {
			redisTemplate.delete(key);
			throw new ValidateCodeException(MsgTemplate.ERROR_VALIDATE_CODE);
		}

		redisTemplate.delete(key);
	}
}
